#include "llLex.hpp"
#include "llReporter.hpp"
using namespace llmake;

#include <sstream>
#include <iostream>
#include <vector>
using namespace std;

void llLexEngine::getLex( llLexList &lexList)
{
	llLexNode lexNode;
	for (llTokenList::iterator iter = m_TokenInput.begin();iter != m_TokenInput.end(); ++iter)
	{
		if ( ")"== iter->m_Token )
		{
			lexNode.m_Tokens.push_back(*iter);
			lexList.push_back(lexNode);
			lexNode.m_Tokens.clear();
		}
		else
		{
			lexNode.m_Tokens.push_back(*iter);
		}
	}
	if( false ==  lexNode.m_Tokens.empty() )
	{
#ifdef DEBUG
		cout<<"size=["<<lexNode.m_Tokens.size()<< "] fist=["<<lexNode.m_Tokens.front().m_Token<<"]"<<endl;
#endif
		std::ostringstream stm;
		stm << "llmake source file->" << __FILE__ << " line->" << __LINE__ <<":";
		string errorInfo( stm.str() + "LLMake Command format wrong " );
		llReporter reporter(errorInfo);
		reporter.append(m_TokenInput);
		reporter.Error();
	}
	for (llLexList::iterator iter = lexList.begin();iter != lexList.end(); ++iter)
	{
//		bool command = false;
		int left = 0;
		int right = 0;
		llTokenList::iterator iterToken = iter->m_Tokens.begin();
		for (;iterToken != iter->m_Tokens.end(); ++iterToken)
		{
			if ( "(" == iterToken->m_Token )
			{
				left ++;
			}
			if ( ")" == iterToken->m_Token )
			{
				right ++;
			}
//			cout<<"<"<<iterToken->m_Token<<" id="<<iterToken->m_Id<<" at line ["<<iterToken->m_Loc.line<<"]>";
		}
		if ( left!= right || 0 == left )
		{
			std::ostringstream stm;
			stm << "llmake source file->" << __FILE__ << " line->" << __LINE__ <<":";
			string errorInfo( stm.str() + "LLMake Command format wrong ");
			llReporter reporter(errorInfo);
			reporter.append(iter->m_Tokens);
			reporter.Error();
		}
	}
}
